//
//  InternetSettingViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-13.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "InternetSettingViewController.h"

@interface InternetSettingViewController ()

@end

@implementation InternetSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"network Settings", @"网络配置 Network Settings");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.sipSegement = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"UDP",@"TCP"]];
    [self.sipSegement setFrame:CGRectMake(163, 83, 137, 30)];
    [self.sipSegement setSelectionIndicatorHeight:3.5f];
    [self.sipSegement setBackgroundColor:[UIColor whiteColor]];
    [self.sipSegement setTextColor:[UIColor colorWithRed:83/255.0 green:215/255.0 blue:105/255.0 alpha:1.0]];
    [self.sipSegement setSelectionIndicatorColor:[UIColor colorWithRed:83/255.0 green:215/255.0 blue:105/255.0 alpha:1.0]];
    [self.sipSegement setSelectionIndicatorMode:HMSelectionIndicatorFillsSegment];
    [self.sipSegement addTarget:self action:@selector(segamentSelectChanged:) forControlEvents:UIControlEventValueChanged];
    [self.sipSegement setSegmentEdgeInset:UIEdgeInsetsMake(0, 6, 0, 6)];
    [self.sipSegement setFont:[UIFont systemFontOfSize:15.0f]];
    [self.scrollView addSubview:self.sipSegement];
    
    
    
    
    
    [self.scrollView setScrollEnabled:YES];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height - 42)];
    
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    
    
    //SIP视频传输协议
    NSString *tempSegmented = [[NSString alloc] init];
    tempSegmented = [defaults objectForKey:@"SAVEVIDEOTRANSPORT"];
    if (tempSegmented.length > 0)
    {
        if ([tempSegmented isEqualToString:@"UDP"])
        {
            [self.sipSegement setSelectedIndex:0];
        }
        else if ([tempSegmented isEqualToString:@"TCP"])
        {
            [self.sipSegement setSelectedIndex:1];
        }
    }
    
    //stun服务器
    NSString *tempstunServer = [[NSString alloc] init];
    tempstunServer = [defaults valueForKey:@"SAVESTUNSERVER"];
    if (tempstunServer.length > 0)
    {
        self.stunTextField.text = tempstunServer;
    }
    
    //SIP协议使用端口
    NSString *tempSipPort = [[NSString alloc] init];
    tempSipPort = [defaults valueForKey:@"SAVESIPPORT"];
    if (tempSipPort.length > 0)
    {
        self.sipPortTextField.text = tempSipPort;
    }
    
    //视频RTP端口
    NSString *tempVideoRTP = [[NSString alloc] init];
    tempVideoRTP = [defaults valueForKey:@"SAVERTPVIDEOPORT"];
    if (tempVideoRTP.length > 0)
    {
        self.videoRTPTextfield.text = tempVideoRTP;
    }
    
    //音频RTP端口
    NSString *tempSoundRTP = [[NSString alloc] init];
    tempSoundRTP = [defaults valueForKey:@"SAVERTPSOUNDPORT"];
    if (tempSoundRTP.length > 0)
    {
        self.soundRTPTextfield.text = tempSoundRTP;
    }
    
    //音视频上传带宽
    NSString *tempSoundAndVideoUp = [[NSString alloc] init];
    tempSoundAndVideoUp = [defaults valueForKey:@"SAVESOUNDANDVIDEOUPWIDTH"];
    if (tempSoundAndVideoUp.length > 0)
    {
        self.soundAndVideoUpTextField.text = tempSoundAndVideoUp;
    }
    
    //音视频下载带宽
    NSString *tempSoundAndVideoDown = [[NSString alloc] init];
    tempSoundAndVideoDown = [defaults valueForKey:@"SAVESOUNDANDVIDEODOWNWIDTH"];
    if (tempSoundAndVideoDown.length > 0)
    {
        self.soundAndVideoDownTextField.text = tempSoundAndVideoDown;
    }
}

- (IBAction)segamentSelectChanged:(HMSegmentedControl *)segmentedControl
{
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (segmentedControl.selectedIndex == 0)
    {
        [interface setVideotransport:1];
        [defaults setObject:@"UDP" forKey:@"SAVEVIDEOTRANSPORT"];
    }
    else if (segmentedControl.selectedIndex == 1)
    {
        [interface setVideotransport:2];
        [defaults setObject:@"TCP" forKey:@"SAVEVIDEOTRANSPORT"];
    }
    [defaults synchronize];
}

- (IBAction)saveButtonPressed:(id)sender
{
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (self.stunTextField.text.length > 0)
    {
        NSString *ip = [[NSString alloc] init];
        ip = self.stunTextField.text;
        [interface setStunServer:ip];
        [defaults setObject:ip forKey:@"SAVESTUNSERVER"];
    }
    else
    {
        [defaults setObject:@"" forKey:@"SAVESTUNSERVER"];
    }
    
    if (self.sipPortTextField.text.length > 0)
    {
        NSString *port = [[NSString alloc] init];
        port = self.sipPortTextField.text;
        [interface setSIPport:port];
        [defaults setObject:port forKey:@"SAVESIPPORT"];
    }
    else
    {
        [defaults setObject:@"" forKey:@"SAVESIPPORT"];
    }
    
    if (self.videoRTPTextfield.text.length > 0)
    {
        NSString *port = [[NSString alloc] init];
        port = self.videoRTPTextfield.text;
        [interface setRTPvideoPort:port];
        [defaults setObject:port forKey:@"SAVERTPVIDEOPORT"];
    }
    else
    {
        [defaults setObject:@"" forKey:@"SAVERTPVIDEOPORT"];
    }
    
    if (self.soundRTPTextfield.text.length > 0)
    {
        NSString *port = [[NSString alloc] init];
        port = self.soundRTPTextfield.text;
        [interface setRTPsoundPort:port];
        [defaults setObject:port forKey:@"SAVERTPSOUNDPORT"];
    }
    else
    {
        [defaults setObject:@"" forKey:@"SAVERTPSOUNDPORT"];
    }
    
    if (self.soundAndVideoUpTextField.text.length > 0)
    {
        NSString *width = [[NSString alloc] init];
        width = self.soundAndVideoUpTextField.text;
        [interface setSoundAndVideoUpWidth:width];
        [defaults setObject:width forKey:@"SAVESOUNDANDVIDEOUPWIDTH"];
    }
    else
    {
        [defaults setObject:@"" forKey:@"SAVESOUNDANDVIDEOUPWIDTH"];
    }
    
    if (self.soundAndVideoDownTextField.text.length > 0)
    {
        NSString *width = [[NSString alloc] init];
        width = self.soundAndVideoDownTextField.text;
        [interface setSoundAndVideoDownWidth:width];
        [defaults setObject:width forKey:@"SAVESOUNDANDVIDEODOWNWIDTH"];
    }
    else
    {
        [defaults setObject:@"" forKey:@"SAVESOUNDANDVIDEODOWNWIDTH"];
    }
    [defaults synchronize];
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)backgroundTouchDown:(id)sender
{
    [self.stunTextField resignFirstResponder];
    [self.sipPortTextField resignFirstResponder];
    [self.videoRTPTextfield resignFirstResponder];
    [self.soundRTPTextfield resignFirstResponder];
    [self.soundAndVideoUpTextField resignFirstResponder];
    [self.soundAndVideoDownTextField resignFirstResponder];
}

- (IBAction)stunTextFieldDidEnd:(id)sender
{
    [self.stunTextField resignFirstResponder];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height  - 42)];
}
- (IBAction)stunTextFieldDidBegin:(id)sender
{
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
}

- (IBAction)sipPortTextFieldDidEnd:(id)sender
{
    [self.sipPortTextField resignFirstResponder];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height  - 42)];
}

- (IBAction)sipPortTextFieldDidBegin:(id)sender
{
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
}


- (IBAction)vidoeRTPTextFieldDidEnd:(id)sender
{
    [self.videoRTPTextfield resignFirstResponder];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height - 42)];
}


- (IBAction)vidoeRTPTextFieldDidBegin:(id)sender
{
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
}

- (IBAction)soundRTPTextFieldDidEnd:(id)sender
{
    [self.soundRTPTextfield resignFirstResponder];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height - 42)];
}

- (IBAction)soundRTPTextFieldDidBegin:(id)sender
{
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
}


- (IBAction)upTextFieldDidEnd:(id)sender
{
    [self.soundAndVideoUpTextField resignFirstResponder];
    
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height - 42)];
}

- (IBAction)downTextFieldDidEnd:(id)sender
{
    [self.soundAndVideoDownTextField resignFirstResponder];
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
}

- (IBAction)upTextFieldDidBegin:(id)sender
{
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
}

- (IBAction)downTextFieldDidBegin:(id)sender
{
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    [self.scrollView setContentSize:CGSizeMake(320, r.size.height + 30)];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
